<script>
import { get } from '@/utils/api'

export default {
  async created () {
    let res = await get('/weapp/test')
    console.log(123, res)
    console.log('小程序启动')
    // this.getUserInfo()
    // 调用API从本地缓存中获取数据
    /*
     * 平台 api 差异的处理方式:  api 方法统一挂载到 mpvue 名称空间, 平台判断通过 mpvuePlatform 特征字符串
     * 微信：mpvue === wx, mpvuePlatform === 'wx'
     * 头条：mpvue === tt, mpvuePlatform === 'tt'
     * 百度：mpvue === swan, mpvuePlatform === 'swan'
     * 支付宝(蚂蚁)：mpvue === my, mpvuePlatform === 'my'
     */

    let logs
    if (mpvuePlatform === 'my') {
      logs = mpvue.getStorageSync({key: 'logs'}).data || []
      logs.unshift(Date.now())
      mpvue.setStorageSync({
        key: 'logs',
        data: logs
      })
    } else {
      logs = mpvue.getStorageSync('logs') || []
      logs.unshift(Date.now())
      mpvue.setStorageSync('logs', logs)
    }
  },
  methods: {
    getUserInfo () {
      wx.getUserInfo({
        withCredentials: true,
        success: function (res) {
          // 此处为获取微信信息后的业务方法
          console.log('获取用户信息成功', res)
        },
        fail: (err) => {
          console.log('获取用户信息失败', err)
          // 获取用户信息失败后。请跳转授权页面
          wx.showModal({
            title: '警告',
            content: '尚未进行授权，请点击确定跳转到授权页面进行授权。',
            success: (res) => {
              console.log('res', res)
              if (res.confirm) {
                // console.log(this.doLogin)
                // this.doLogin()
                console.log('用户点击确定')
                wx.navigateTo({
                  url: '/pages/logs/main'
                })
              }
            }
          })
        }
      })
    }
  },
  log () {
    console.log(`log at:${Date.now()}`)
  }
}
</script>

<style>
.page-title{
  /* padding-left:20px; */
  background:#eee;
  line-height: 40px;
  font-size: 14px;
  text-align: center;
}
.container {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 200rpx 0;
  box-sizing: border-box;
}
/* this rule will be remove */
* {
  transition: width 2s;
  -moz-transition: width 2s;
  -webkit-transition: width 2s;
  -o-transition: width 2s;
}
.text-footer{
  text-align: center;
  font-size: 12px;
  margin-bottom: 10px;
}
.text-primary{
  color:#EA5149;
}
.btn{
  color:white;
  background:#EA5A49;
  margin-bottom: 10px;
  padding-left: 15px;
  padding-left: 15px;
  border-radius: 2px;
  font-size: 16px;
  line-height: 40px;
  height: 40px;
  width: 100%;
}
.btn:active{
  background: #FA5A49;
}
</style>
